<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--device first-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>分公司管理</title>
<script src="/res/js/common/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/js/structure/struture.js" type="text/javascript"></script> 
<script src="/res/js/common/common.js" type="text/javascript"></script>
<script src="/res/third/layer/layer.js" type="text/javascript" ></script>
<link href="/res/css/respective/role/role.css" rel="stylesheet">
<style type="text/css">
	.n-default .n-left, .n-default .n-right {
    margin-top: 15px;
}
</style>
</head>
[#assign menucode="branchManage"/]
<body>
	<div class="warp">
		[#include "/front/common/front_headAzt.html"]
		<div class="backcontent">
			[#include "/front/common/front_leftAzt.html"]
			<div class="back-main">
              <form id="branchForm" onsubmit="return false">
				<div class="inner">
					<div class="mainContent">
						<div class="mainContentWhite">
							<h1>分公司管理</h1>
							<hr>
							<div class="branchInfo">
							    <input id="branchCompanyId" type="hidden" value="[#if branchcompany??]${branchcompany.id!}[/#if]"  /> 
							    <input id="oldmobile" name="oldmobile" type="hidden" value="[#if userAdmin??]${userAdmin.mobile!}[/#if]">
								<div>
									<label><span>*</span>分公司名称：</label> <input type="text" id="branchCompanyName" name="branchCompanyName" maxlength="30" value="[#if branchcompany??]${branchcompany.companyName!}[/#if]"
										placeholder="如“南京分公司”" />
								</div>
								<div>
									<label><span>*</span>登录账号：</label> <input type="text" id="mobile1" name="mobile1" value="[#if userAdmin??]${userAdmin.mobile!}[/#if]" 
										placeholder="请输入登录手机号" />
								</div>
								<div>
									<label><span>*</span>确认账号：</label> <input type="text" id="mobile2" name="mobile2"  value="[#if userAdmin??]${userAdmin.mobile!}[/#if]" onpaste="return false" oncontextmenu="return false" oncopy="return false" oncut="return false"
										placeholder="请再次输入登录手机号" />
								</div>
								<div>
									<label><span>*</span>分公司负责人：</label><span style="color: #666666" id="hiddenRealName"></span> <input type="text" id="realName" name="RealName" maxlength="16" value="[#if userAdmin??]${userAdmin.realName!}[/#if]"
										placeholder="请填写负责人姓名" />
								</div>
								[#if company.parentId??]<!-- 用二级分公司登录进来没权限不显示 -->
								[#else]
								<div>
									<label><span>*</span>上级管理公司：</label> <!-- 公司是自己就是创建二级公司 -->
									<div class="ppSelect" style="width: 260px;">
										<select id="parentCompany" name="parentCompany">
						                [#if allcompany?? && allcompany?size>0]
						                [#list allcompany as allcompany]
						                <option value="${allcompany.id!}" [#if branchcompany?? && branchcompany.parentId == allcompany.id ]selected='selected'[/#if]>${allcompany.companyName!}[#if company?? && company.id==allcompany.id](本公司)[/#if]</option>
						                [/#list]
						                [/#if]
						                </select>
									</div> 
					            </div>
					            [/#if]
							</div>
							<h2>
								<span>*</span>分公司负责人权限
							</h2>
			                    [#if loadMenuTree??]
			                    [#list loadMenuTree as par]
							<div class="itermBorder">
							<!-- 分公司没有设置或未选(-1)的时候显示-->
							   <b [#if branchcompany??][#if par.mark==1 ]style="display: none;"[/#if][#else]style="display: none;"[/#if]> <i class="noChoose"></i> <bdo
										data-type="fun_${par.code!}">
										<strong>${par.name!}</strong>
										<p>${par.descr!}</p>
									</bdo>
								</b>
								
								<div class="verAlignBdoDiv" [#if branchcompany??][#if par.mark!=1  ]style="display: none;"[/#if][/#if] >
									<div class="verAlignBdo">
										<b class="tbPad"> <i class="choose" parentId="${par.id!}"></i> <bdo
												data-type="fun_${par.code!}">
												<strong>${par.name!}</strong> <small>${par.descr!}</small>
											</bdo>
										</b>
									</div>
									<div class="power">
									[#list par.children as child]
									[#if company.parentId?? && child.code=='BranchManage' || child.code=='CentralizedManage']
									[#else]
										<i data-type="fun_${child.code!}" childcode="${child.code!}" childid="${child.id!}">  <img class="visimg" [#if child.mark!=0 ]style="visibility: visible;"[#else]style="visibility: hidden;"[/#if] src="/res/images/role/b_03.png" />
											<p>${child.name!}</p>
										</i>
									[/#if]	
									[/#list]
									</div>
								</div>
							</div>
							[/#list]
						  [/#if]
			                    <button class="sav" type="button" onclick="savebranch();" style="padding: 8px 20px;">保存</button>
							</div> 
						</div>
					</div>
					</form>
				</div>
			</div>
			<!-- 底部菜单-->
			[#include "/front/common/front_footerAzt.html"]
		</div> 
	</div>
	<script src="/res/third/nice-validator/jquery.validator.js?local=zh-CN" type="text/javascript"></script>
	<script type="text/javascript">
	$(function(){
		branchManage();
	});
	
	function branchManage(){
		var parentval=$("#parentCompany").val();
		var companyId='${company.id!}';
		if(parentval!=companyId){
			$(".power i").each(function(){
				if($(this).attr("childcode")=='BranchManage'){
					$(this).hide();
				}
			});
		}else{
			$(".power i").each(function(){
				if($(this).attr("childcode")=='BranchManage'){
					$(this).show();
				}
			});
		}
	}
	
		$("#branchForm").validator({
			    theme: 'newsimple_right',
			    timely: 1,
			    stopOnError: true,
			    rules: {
			    	companyname:function(){
			    		var name=$("#branchCompanyName").val();
			    		var regx=/^[\u4e00-\u9fa5a-zA-Z0-9]+$/;
			    		if(regx.test(name)){
			    			var glength=gblen(name);
			    			console.log(glength);
				    		if(glength>30){//中文15字，英文30字
				    			return "输入分公司名称，支持中英文数字，最多15字";
				    		}
			    		}else{
			    			return "输入分公司名称，支持中英文数字，最多15字";
			    		}
			    	},
			    	username:function(){
			    		var name=$("#realName").val();
			    		var regx=/^[\u4e00-\u9fffa-zA-Z]+$/;
			    		if(regx.test(name)){
			    			var glength=gblen(name);
				    		if(glength>16){//中文8字，英文16字
				    			return "输入分公司负责人的名字，支持中英文，最多8字";
				    		}
			    		}else{
			    			return "输入分公司负责人的名字，支持中英文，最多8字";
			    		}
			    	},
			        mobile: [/^1[3-9]\d{9}$/, "请填写有效的手机号"],
			        vidateCompanyName:function(){
			        	var branchCompanyId=$("#branchCompanyId").val();
			         	return $.ajax({
							type : "post",
							dataType : "json",
							url : "/orgstruct/validateNameOrMobile",
							data :{'branchName' :$.trim($("#branchCompanyName").val()),'branchCompanyId':branchCompanyId},
							success : function(data) {//成功的时候调用
							}
						});
			        },
		            vidatemobile:function(){
		            	var branchCompanyId=$("#branchCompanyId").val();
		            	return $.ajax({
							type : "post",
							dataType : "json",
							url : "/orgstruct/validateNameOrMobile",
							data :{'mobile':$.trim($("#mobile1").val()),'branchCompanyId':branchCompanyId},
							success : function(data) {
								//成功的时候调用
								if(data.obj!=3){
									if(data.realName!='' && data.realName!=null){
										$("#hiddenRealName").text(data.hiddenrealName);
										$("#hiddenRealName").show();
										$("#realName").val(data.realName);
										$("#realName").attr("type","hidden");
									}else{
										$("#realName").val('');
										$("#realName").attr("type","text");
										$("#hiddenRealName").hide();
									}
								}else{
									$("#realName").val('');
									$("#realName").attr("type","text");
									$("#hiddenRealName").hide();
								}
							}
						});
		            }
			    },
			    fields: {
			    	'branchCompanyName':{
			    		rule: "required;vidateCompanyName;companyname",
			    		msg: {required: "必填"}
			    	},
			    	'RealName':"required;username",
			        'mobile1': "required; mobile;vidatemobile",
			        'mobile2':{
			        	rule:"required; mobile;match(mobile1)",
			        	msg: {match: "账号输入不一致!"}
			        },
			        'parentCompany':"required"
			    },
		        messages: {
		        required: "必填",
		        }
		});
 	
		//判断字符串长度
 function gblen(name) {  
	var len = 0;  
	for (var i=0; i<name.length; i++) {  
		if (name.charCodeAt(i)>127 || name.charCodeAt(i)==94) {  
			 len += 2;  
		 } else {  
			 len ++;  
		 }  
	 }  
	return len;  
  }
		
		
		//验证
	function savebranch(){
		var data={};
		var information=[];
    	var parent={};
		$(".itermBorder").each(function(){
			var child=[];
			var verAlignBdoDiv=$(this).find(".verAlignBdoDiv :visible");
			if(verAlignBdoDiv.length>0){
				var childcount=0;
				var parentId=verAlignBdoDiv.find(".choose").attr("parentId");
				$(this).find(".power i").each(function(){
					//判断子存在
					if($(this).children("img").css("visibility")!="hidden"){
						child.push($(this).attr("childid"));
						childcount++;
					}
				});
				if(childcount>0){
					parent[parentId]=child;
				}
			}
		});
		
		data.parent=parent;
		data.parentCompany=$("#parentCompany").val();
		data.branchCompanyId=$("#branchCompanyId").val();
		data.branchCompanyName=$.trim($("#branchCompanyName").val());
		data.realName=$.trim($("#realName").val());
		data.mobile1=$.trim($("#mobile1").val());
		data.oldmobile=$.trim($("#oldmobile").val());
		
		var parentlength=false;
	    for (var key in parent) { 
	    	parentlength=true;
	    	continue;
	    } 
		
		$("#branchForm").isValid(function(){
			if(!parentlength){
				layer.msg("请选择分公司负责人权限" , {time:2000} );
				return;
			}
			     var me = true;
			       if(me){
			    	$.ajax({
						type : "post",
						dataType : "json",
						//contentType:"application/json",
						url : "/orgstruct/saveBranchCompany",
						data :{'data' :JSON.stringify(data)},
						success : function(data) {//成功的时候调用
							if(data.success){
								 me=false;
								 location.href="/orgstruct/listBranchCompany";
							}else{
								 me=true;
								 layer.msg(data.msg , {time:2000});
							}
						}
					});
			      }
		});
	}	
	</script>
</body>
</html>